package com.shop.cereshop.app.service.group.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.app.dao.group.CereShopGroupDAO;
import com.shop.cereshop.app.service.group.CereShopGroupService;
import com.shop.cereshop.commons.domain.shop.CereShopGroup;
import com.shop.cereshop.commons.domain.shop.bo.CereShopGroupBo;
import com.shop.cereshop.commons.domain.shop.vo.CereShopGroupVo;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CereShopGroupServiceImpl extends ServiceImpl<CereShopGroupDAO, CereShopGroup> implements CereShopGroupService {
    /**
     * 条件构造器
     */
    public LambdaQueryWrapper<CereShopGroup> buildLambdaQueryWrapper(CereShopGroupBo param){
        LambdaQueryWrapper<CereShopGroup> q1 = new LambdaQueryWrapper<>();
        q1.eq(ObjectUtil.isNotEmpty(param.getShopId()),CereShopGroup::getShopId,param.getShopId());
        q1.eq(ObjectUtil.isNotEmpty(param.getSuperGroupId()),CereShopGroup::getSuperGroupId,param.getSuperGroupId());
        q1.eq(ObjectUtil.isNotEmpty(param.getIsMenu()),CereShopGroup::getIsMenu,param.getIsMenu());
        q1.orderByDesc(CereShopGroup::getSort);
        return q1;
    }

    /**
     * 条件查询
     */
    public List<CereShopGroupVo> queryList(CereShopGroupBo param){
        List<CereShopGroup> list = list(buildLambdaQueryWrapper(param));
        List<CereShopGroupVo> voList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(list)){
            voList =  generate(list);
        }
        return voList;
    }

    /**
     * 转换函数
     */
    public List<CereShopGroupVo> generate(List<CereShopGroup> list){
        List<CereShopGroupVo> shopGroupVos = Lists.newArrayList();
        for (CereShopGroup shopGroup : list) {
            CereShopGroupVo cereShopGroupVo = new CereShopGroupVo();
            BeanUtils.copyProperties(shopGroup,cereShopGroupVo);
            shopGroupVos.add(cereShopGroupVo);
        }
        return shopGroupVos;
    }

    /**
     * 转换函数
     */
    public CereShopGroupVo generate(CereShopGroup group) {
        CereShopGroupVo cereShopGroupVo = new CereShopGroupVo();
        BeanUtils.copyProperties(group,cereShopGroupVo);
        return cereShopGroupVo;
    }
}
